﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace MPSystem.Util
{
    /// <summary>
    /// 数字转换器
    /// </summary>
    [ValueConversion(typeof(double), typeof(string))]
    public class StringConverter : IValueConverter
    {
        //实现接口的两个方法
        #region IValueConverter 成员
        /// <summary>
        /// 格式化字符串
        /// 特别的，当数据为double类型，且Formatter为f+时，负值会被转换为"—"
        /// </summary>
        public string Formatter { get; set; }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "—";
            else if (value is double)
            {
                if (Formatter == "f+")
                {
                    double v = (double)value;
                    return v<0? "—" : v.ToString("f");
                }
                else
                    return ((double)value).ToString(Formatter);
            }
            else if (value is DateTime)
                return ((DateTime)value).ToString(Formatter);
            return value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return double.Parse(value.ToString());
        }
        #endregion

        /// <summary>
        /// 根据操作系统进行DateTime格式化
        /// yyyy-MM-dd/hh:mm:ss
        /// </summary>
        /// <returns></returns>

        public string FormateDateForSystemPlat(DateTime dt)
        {
            if (OSHelper.IsWindows2003 ||
                OSHelper.IsWindows2008 || OSHelper.IsWindows2008R2 ||
                OSHelper.IsWindows2012 || OSHelper.IsWindows2012R2)
            {
                return dt.ToString("yyyy\\/MM\\/dd HH:mm:ss");
            }
            else
                return dt.ToString("yyyy/MM/dd HH:mm:ss");

        }
    }
}
